import io, re
from .utils import post
from .wrapper import subthread, withWaitSound
from . import settings
import ui
URL = 'http://fund.eastmoney.com/%s.html?spm=search'
# 天天基金的查询接口(1234567.com.cn)
def getFundInfo(code):
    # 获取一个基金的信息，返回 html
    res = post(URL % code).decode()
    # 解析原始 html
    buff = io.StringIO()
    info = re.search('<div\s+class=\"fundDetail-tit\">(<div[^>]+>.+?</div>[^<]+)', res, flags=re.I)
    if info: #基本信息
        buff.write('<h1>' + re.sub('<[^>]+>', '', info.group(1), flags=re.I) + '</h1>')
    floatList = re.findall('<tr>\s+<td[^>]+>([^<]+)</td>\s+<td[^>]+>([^<]+)</td>\s+<td[^>]+>([^<]+)</td>\s+<td[^>]+><span[^>]+>([^<]+)</span>\s+</td>', res, flags=re.I)
    if floatList: # 净值数据
        buff.write('<table title="净值表">') # 表格开始
        buff.write('<tr>') # 表头开始
        [buff.write(f'<th>{i}</th>') for i in ('日期', '单位净值', '累计净值', '日涨幅')]
        buff.write('</tr>') # 表头结束
        for item in floatList:
            buff.write('<tr>') # 行开始
            [buff.write(f'<td>{i}</td>') for i in item]
            buff.write('</tr>') # 行结束
        buff.write('</table>') # 表格结束
    floatList = re.findall('<dd><span>(.+?)</span><span[^>]+>(.+?)</span></dd>', res, flags=re.I)
    if floatList: # 涨幅数据
        for item in sorted(floatList, key=lambda x:(10 if x[0][1]=='立' else int(x[0][1])) + (10 if x[0][2]=='月' else 100)):
            buff.write(f'<p>{item[0]} {item[1]}</p>')
    info = re.search('<div\s+class=\"infoOfFund\"><div[^>]+>[^<]*</div>(.+?)</div>', res, flags=re.I)
    if info: #更多信息
        infoList = re.findall('<td>(.+?)</td>', info.group(1), flags=re.I)
        for item in infoList:
            buff.write('<p>' + re.sub('<[^>]+>|&nbsp;', '', item, flags=re.I) + '</p>')
    info = re.search(r'<div\s+class="barEssayListWrap"><table[^>]+>(.+?)</table>', res, flags = re.I)
    if info: #评论信息
        buff.write('<table title="评论表">') # 表格开始
        buff.write('<tr>') # 表头开始
        [buff.write(f'<th>{i}</th>') for i in ('日期', '用户名', '内容', '点击量', '回复数', '原链接')]
        buff.write('</tr>') # 表头结束
        essayList = re.findall(r'<tr>\s*<td[^>]+>([^<]+)</td>\s*<td[^>]+>([^<]+)</td>\s*<td[^>]+>\s*<a[^>]+href="([^"]+)"[^>]+title="([^"]+)"[^>]*>[^<]+</a>\s*</td>\s*<td[^>]+>\s*<a[^>]+>([^<]+)</a>\s*</td>\s*<td[^>]+>([^<]+)</td>\s*</tr>', info.group(1), flags = re.I)
        for click, reply, href, content, author, date in essayList:
            buff.write('<tr>') # 行开始
            buff.write(f'<td>{date}</td>')
            buff.write(f'<td>{author}</td>')
            buff.write(f'<td>{content}</td>')
            buff.write(f'<td>{click}</td>')
            buff.write(f'<td>{reply}</td>')
            buff.write(f'<td>{href}</td>')
            buff.write('</tr>') # 行结束
        buff.write('</table>') # 表格结束
    res = buff.getvalue()
    buff.close()
    if not res:
        raise Exception('No result.')
    return res
currentFund = 0 #当前基金序号
@withWaitSound
@subthread
def viewFund():
    global currentFund
    # 读取用户关注的基金列表
    codeList = settings.getViewFund()
    code = codeList[currentFund]
    # 循环切换基金的处理
    currentFund += 1
    if currentFund >= len(codeList):
        currentFund = 0
    if not code:
        ui.message(_('请先在设置中添加基金代码。'))
        return
    try:
        res = getFundInfo(code)
        ui.browseableMessage(res, f'基金详情({code})', True)
    except Exception as e:
        ui.message(_(f'{e}'))